package com.xauto.pioneer.appuser.activity.mine.balance;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.adapter.TransactionAdapter;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.interfaces.MemberCallBack;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.Member;
import com.xauto.pioneer.appuser.model.PageInfo;
import com.xauto.pioneer.appuser.model.TransactionInfo;
import com.xauto.pioneer.appuser.util.DataLoadUtil;
import com.xauto.pioneer.appuser.util.ViewUtil;
import com.xauto.pioneer.appuser.view.TopBar;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/***
 * 账户余额 页面
 * @author Lemon Tue 7 Mar 17:14
 */
public class AccountBalanceActivity extends BaseActivity {

    @Bind(R.id.topBar) TopBar topBar;
    @Bind(R.id.tvAccountBalance) TextView tvAccountBalance;
    @Bind(R.id.list)
    PullToRefreshListView pullToRefreshListView;
    @Bind(R.id.tvAccountTopUp)
    TextView tvAccountTopUp;
    @Bind(R.id.tvAccountWithdraw)
    TextView tvAccountWithdraw;
    private ListView list;
    /** 数据适配器*/
    private TransactionAdapter adapter;
    /** 数据集合*/
    private List<TransactionInfo> transactionInfoList;
    private Member member;
    @Override
    protected int getContentView() {
        return R.layout.ac_account_balance;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setText(TopBar.TITLE, R.string.account_balance);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        list = pullToRefreshListView.getRefreshableView();
        list.setVerticalScrollBarEnabled(false);
        list.setDividerHeight(3);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //刷新
                page = 1;
                load();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //下拉
                if (isMore){
                    page ++;
                    load();
                }else {
                    pullToRefreshListView.onRefreshComplete();
                    prompt("暂无更多数据");
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        //刷新页面
        DataLoadUtil.refreshMemberData(mContext, new MemberCallBack() {
            @Override
            public void getMember(Member m) {
                member = m;
                setView();
            }
        });
        load();
    }
void setView(){
    tvAccountBalance.setText("¥"+member.member_account_info.balance_amount+"元");
}
    @Override
    protected void initData() {
        lHandler = new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                pullToRefreshListView.onRefreshComplete();
            }

            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                pullToRefreshListView.onRefreshComplete();
                JSONArray jsonArray = jsonStatus.data.optJSONArray("member_transaction_list");
                isMore = jsonStatus.pageInfo.more == 1 ? true : false;
                if(jsonArray != null && jsonArray.length() > 0) {
                    transactionInfoList = new ArrayList<>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        transactionInfoList.add(new TransactionInfo().parse(jsonArray.optJSONObject(i)));
                    }
                    refreshList();
                }else {
                    showNoDataView();
                }
            }
        };

        adapter = new TransactionAdapter(this, R.layout.item_transaction);
        list.setAdapter(adapter);
        page = 1;
    }
    void load() {
        new LHttpLib().getTransacitonList(mContext,memberId, new PageInfo(page, Constants.COUNT), lHandler);
    }

    void refreshList() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (page == 1){adapter.getDataList().clear();}
                adapter.getDataList().addAll(transactionInfoList);
                adapter.notifyDataSetChanged();
                tvAccountWithdraw.setClickable(true);
                ViewUtil.GetViewShgape(tvAccountWithdraw);
                tvAccountWithdraw.setBackgroundResource(R.drawable.bg_btn_green_selector);
                ViewUtil.SetViewShgape(tvAccountWithdraw);
            }
        });
    }
    private void showNoDataView(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                View emptyView = LayoutInflater.from(mContext).inflate(R.layout.layout_listview_empty, null);
                ((TextView) emptyView.findViewById(R.id.tvEmptyNotice)).setText("当前无记录");
                ((ViewGroup)list.getParent()).addView(emptyView);
                list.setEmptyView(emptyView);

                tvAccountWithdraw.setClickable(false);
                ViewUtil.GetViewShgape(tvAccountWithdraw);
                tvAccountWithdraw.setBackgroundResource(R.drawable.bg_btn_gray);
                ViewUtil.SetViewShgape(tvAccountWithdraw);
            }
        });
    }
    @OnClick({R.id.tvAccountTopUp, R.id.tvAccountWithdraw})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvAccountTopUp:
                //账户余额充值
                startActivity(new Intent(mContext, AccountTopUpActivity.class));
                break;
            case R.id.tvAccountWithdraw:
                //账户余额提现
                startActivity(new Intent(mContext, AccountWithdrawActivity.class));
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
